import React, { Component } from 'react'
import PropTypes from 'prop-types'
import {
  Form,
  Input
} from 'antd/lib/index'

const Item = Form.Item

// 更新分类的组件
class UpdateForm extends Component {
  static propTypes = {
    categoryName: PropTypes.string.isRequired, // 当前分类名字
    setForm: PropTypes.func.isRequired, // 用来传递 form 对象的函数
  }

  UNSAFE_componentWillMount () {
    // 将 from对象通过 setForm() 传递给父组件
    this.props.setForm(this.props.form)
  }

  render () {
    const { categoryName } = this.props

    // 获取 form 对象
    const { getFieldDecorator } = this.props.form

    return (
      <Form onSubmit={this.handleSubmit}>
        <Item>
          {
            getFieldDecorator('categoryName', {
              initialValue: categoryName,
              rules: [
                { required: true, message: '分类名称必须输入' }
              ]
            })(
              <Input placeholder='请输入分类名称' />
            )
          }
        </Item>
      </Form>
    )
  }
}

export default Form.create()(UpdateForm)
